/***************************************************************************
 * File name: q2.c
 *
 * Authors:  Tomer Filiba		Reuven Horon
 *           037013075			066729740
 *           sebulba@t2			sreuvenh@t2
 *
 * Exercise #: 6
 * Question #: 2
 * Submission Date: 29/8/07
 **************************************************************************/

/*-------------------------------------------------------------------------
  Include files:
--------------------------------------------------------------------------*/
#include <stdio.h>

/*-------------------------------------------------------------------------
  Constants, definitions and globals:
--------------------------------------------------------------------------*/
#define SUCCESS		(0)
#define FAILURE		(1)
#define N			(9)

typedef enum 
{
	TRUE = (0 == 0), 
	FALSE = (1 == 0)
} bool_t;

int array[N] = {2, 4, 1, 4, 0, -2, 4, 2, 16};

/*-------------------------------------------------------------------------
  Forward declarations:
--------------------------------------------------------------------------*/
void printOnlyOneTime(int arr[N]);
void uniquify(int arr[], int i, int len);
bool_t is_unique(int value, int arr[], int len);

/*-------------------------------------------------------------------------
  The main program: prints only the unique elements in the array
 -------------------------------------------------------------------------*/
int main()
{
	printOnlyOneTime(array);
	return SUCCESS;
}

/*-------------------------------------------------------------------------
  Prints each element in the array only once (API function)

  Parameters:
  arr - the array of size N (constant)

  Return value: none
 -------------------------------------------------------------------------*/
void printOnlyOneTime(int arr[N])
{
	uniquify(arr, 0, N);
	printf("\n");
}

/*-------------------------------------------------------------------------
  Prints each element in the array only once (recursive calls)

  Parameters:
  - arr - the array of size N (constant)
  - i - the index to examine
  - len - the length of the array

  Return value: none
 -------------------------------------------------------------------------*/
void uniquify(int arr[], int i, int len)
{
	if (i >= len)
		return;
	if (is_unique(arr[i], arr, i))
		printf("%d ", arr[i]);
	/* uniquify the rest */
	uniquify(arr, i + 1, len);
}

/*-------------------------------------------------------------------------
  Determines whether the given value is unique (meaning, it does not 
  already appear in the array)

  Parameters:
  - value - the value to look for
  - arr - the array
  - len - the length of the array

  Return value: TRUE if unique, FALSE if already appearing
 -------------------------------------------------------------------------*/
bool_t is_unique(int value, int arr[], int len)
{
	if (len == 0)
		return TRUE;
	if (arr[0] == value)
		return FALSE;
	return is_unique(value, arr + 1, len - 1);
}

/**************************************************************************
			                          End of file
***************************************************************************/